package com.jieli.stream.dv.running2.ui.activity;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.SurfaceView;

import com.jieli.lib.dv.control.connect.response.SendResponse;
import com.jieli.lib.dv.control.json.bean.NotifyInfo;
import com.jieli.lib.dv.control.receiver.listener.OnNotifyListener;
import com.jieli.lib.dv.control.utils.Code;
import com.jieli.lib.dv.control.utils.Topic;
import com.jieli.lib.dv.control.utils.TopicKey;
import com.jieli.lib.dv.control.utils.TopicParam;
import com.jieli.stream.dv.running2.R;
import com.jieli.stream.dv.running2.camera.SurfaceViewCallback;
import com.jieli.stream.dv.running2.ui.base.BaseActivity;
import com.jieli.stream.dv.running2.util.ClientManager;
import com.jieli.stream.dv.running2.util.Dbug;
import com.jieli.stream.dv.running2.util.ToastUtil;

import static com.jieli.lib.dv.control.utils.Constants.SEND_SUCCESS;

public class CameraActivity extends BaseActivity {
    private String tag = getClass().getSimpleName();
    private SurfaceView surfaceView;
    private final int CAMERA_OK = 100;
    private SurfaceViewCallback mCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);

        ClientManager.getClient().registerNotifyListener(onNotifyListener);

        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                //先判断有没有权限 ，没有就在这里进行权限的申请
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_OK);
            }else {
                //说明已经获取到摄像头权限了 想干嘛干嘛
                initCamera();
            }
        }else {
            //这个说明系统版本在6.0之下，不需要动态获取权限。
            initCamera();
        }
    }


    private void initCamera() {
        mCallback = new SurfaceViewCallback(CameraActivity.this);
        surfaceView.getHolder().addCallback(mCallback);
        ClientManager.getClient().tryToStreamingPush(true, PROJECTION_WIDTH, PROJECTION_HEIGHT, 30, new SendResponse() {
            @Override
            public void onResponse(Integer code) {
                if (code != SEND_SUCCESS) {
                    Dbug.e(tag, "send failed!!!");
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case CAMERA_OK:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //这里已经获取到了摄像头的权限，想干嘛干嘛了可以
                    initCamera();
                }else {
                    //这里是拒绝给APP摄像头权限，给个提示什么的说明一下都可以。
                    ToastUtil.showToastLong("请手动打开相机权限");
                }
                break;
            default:
                break;
        }
    }

    private final OnNotifyListener onNotifyListener = new OnNotifyListener() {
        @Override
        public void onNotify(NotifyInfo data) {
            if (data.getErrorType() != Code.ERROR_NONE) {
                Dbug.e(tag, Code.getCodeDescription(data.getErrorType()));
                return;
            }
            switch (data.getTopic()) {
                case Topic.NET_SCR:
                    Dbug.w(tag, " getTopic="+ data.getTopic());
                    if (null != data.getParams()) {
                        boolean isOpen = TopicParam.OPEN.equals(data.getParams().get(TopicKey.STATUS));
                        Dbug.w(tag, "isOpen "+ isOpen);
                        if (isOpen) {
                            mCallback.setDeviceState(true);
                        } else {
                            mCallback.setDeviceState(false);
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        ClientManager.getClient().tryToStreamingPush(false, 0, 0, 0, new SendResponse() {
            @Override
            public void onResponse(Integer code) {
                if (code != SEND_SUCCESS) {
                    Dbug.e(tag, "send failed!!!");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ClientManager.getClient().unregisterNotifyListener(onNotifyListener);
    }
}
